<template>
  <div class="dept-card card" :class="{'animate': animated}">
    <div class="dept-image" :style="{ backgroundImage: `url(${dept.image})` }"></div>
    <div class="dept-info">
      <h3 class="dept-title">{{ dept.name }}</h3>
      <p class="dept-description">
        {{ dept.description }}
      </p>
      <div class="doctors-list">
        <span class="doctor-tag" v-for="doctor in dept.doctors" :key="doctor.name">
          <i class="fas fa-user-md"></i> {{ doctor.name }}
        </span>
      </div>
    </div>
    <button class="action-button" @click="goToDepartment">
      预约科室
    </button>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { useIntersectionObserver } from '@vueuse/core'
import { useRouter } from 'vue-router'

interface Doctor {
  name: string
}

interface Department {
  id: number
  name: string
  description: string
  image: string
  doctors: Doctor[]
}

const props = defineProps<{
  dept: Department
  delay?: number
}>()

const router = useRouter()
const animated = ref(false)
const target = ref<HTMLElement | null>(null)

// 延迟动画
const delay = props.delay || 0

// 使用交叉观察器实现滚动动画
const { stop } = useIntersectionObserver(
  target,
  ([{ isIntersecting }]) => {
    if (isIntersecting) {
      setTimeout(() => {
        animated.value = true
      }, delay * 1000)
      stop()
    }
  },
  { threshold: 0.15 }
)

// 导航到科室详情页
const goToDepartment = () => {
  router.push({ name: 'DepartmentDetail', params: { id: props.dept.id } })
}
</script>

<style scoped lang="scss">
.dept-card {
  height: 100%;
  
  .dept-image {
    height: 180px;
    background-size: cover;
    background-position: center;
  }
  
  .dept-info {
    padding: 20px;
  }
  
  .dept-title {
    font-family: 'Poppins', sans-serif;
    font-size: 1.3rem;
    font-weight: 600;
    margin-bottom: 10px;
    color: var(--text-dark);
  }
  
  .dept-description {
    color: var(--text-medium);
    margin-bottom: 15px;
    font-size: 0.95rem;
  }
  
  .doctors-list {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-top: 15px;
  }
  
  .doctor-tag {
    padding: 6px 12px;
    background: var(--light-blue);
    border-radius: 20px;
    font-size: 0.85rem;
    color: var(--primary-blue);
    display: inline-flex;
    align-items: center;
    gap: 6px;
  }
  
  .action-button {
    width: 100%;
    text-align: center;
    padding: 12px;
    background: var(--primary-blue);
    color: var(--white);
    border: none;
    border-radius: 0 0 20px 20px;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
    
    &:hover {
      background: var(--dark-blue);
    }
  }
}
</style>
